﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Messaging;

public partial class site_mag_ViewQueue : System.Web.UI.Page
{
    static IMessageFormatter formatter = new System.Messaging.BinaryMessageFormatter();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string appcode = this.Request["appcode"];
            if (string.IsNullOrWhiteSpace(appcode))
                return;

            Maticsoft.BLL.bll_api_delay api_delay = new Maticsoft.BLL.bll_api_delay();
            var app = api_delay.GetModelByCacheWithAppCode(appcode);

            MessageQueue queue = new MessageQueue(app.appqueue);
            System.Messaging.Message[] messages = queue.GetAllMessages();

            if (messages.Length == 0)
            {

                return;
            }
            

            List<QueueItem> list = new List<QueueItem>();

            foreach (System.Messaging.Message message in messages)
            {
                message.Formatter = formatter;

                QueueItem item = new QueueItem();
                item.Title = message.Label;
                item.Body = message.Body.ToString();

                list.Add(item);
            }

            ListBox1.DataSource = list;
            ListBox1.DataValueField = "Body";
            ListBox1.DataTextField = "Title";
            ListBox1.DataBind();

        }

    }

    public class QueueItem
    {
        public string Title { get; set; }

        public string Body { get; set; }

    }

    protected void btnView_Click(object sender, EventArgs e)
    {

        if (ListBox1.SelectedItem == null)
            TextBox1.Text = "";
        else
            TextBox1.Text = ListBox1.SelectedItem.Value;


    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        this.Response.Redirect("API_DelayList.aspx");
    }
}